package lc20240422;

import java.io.FileWriter;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CoordinateCalculator {
    public static void main(String[] args) {
        String input = "A10;W20;S10;D30;W40";
        String[] coordinates = input.split(";");

        int x = 0;
        int y = 0;

        Pattern pattern = Pattern.compile("([ADWS])(\\d{1,2})");

        for (String coordinate : coordinates) {
            Matcher matcher = pattern.matcher(coordinate);
            if (matcher.matches()) {
                String direction = matcher.group(1);
                int distance = Integer.parseInt(matcher.group(2));
                switch (direction) {
                    case "A":
                        x -= distance;
                        break;
                    case "D":
                        x += distance;
                        break;
                    case "W":
                        y += distance;
                        break;
                    case "S":
                        y -= distance;
                        break;
                }
            }
        }

        String output = "最终坐标为：(" + x + ", " + y + ")";
        System.out.println(output);

        // 输出结果到文件
        try {
            FileWriter writer = new FileWriter("output.txt");
            writer.write(output);
            writer.close();
            System.out.println("结果已写入到 output.txt 文件中。");
        } catch (IOException e) {
            System.out.println("写入文件时出现错误：" + e.getMessage());
        }
    }
}